﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Giaiphongmatbang.DataModel;
using Giaiphongmatbang.BLL;
namespace Giaiphongmatbang
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        static List<Table_1> list = new List<Table_1>();
        QLGPMBEntities db = new QLGPMBEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                list = getParent();
                BindData();   
            }
        }
        
        List<Table_1> getParent()
        {
            return db.Table_1.Where(p => p.ParentID == 0).ToList();
        }

        List<Table_1> getChild(int ParentID)
        {
            return db.Table_1.Where(p => p.ParentID == ParentID).ToList();
        }

        List<Table_1> getChildList(int ParentID)
        {
            return list.Where(p => p.ParentID == ParentID).ToList();
        }
        string img(int id)
        {
            if(!db.Table_1.Any(p=>p.ParentID == id))
                return "<img src='/Images/folder.png'/>";
            if(!list.Any(p=>p.ParentID == id))
                return "<a href='javascript:expand("+id+");'><img src='/Images/expand.png'/></a>";
            return "<a href='javascript:collapse("+id+");'><img src='/Images/collape.png'/></a>";
        }
        protected void BindData()
        {
            StringBuilder str = new StringBuilder("<table width='100%' border='1' color='red'>");
            foreach (var item in list.Where(p=>p.ParentID == 0))
            {
                StringBuilder node = new StringBuilder("<tr><td>" + img(item.ID) + item.Name + "</td><td>" + item.ID + "</td><td><a href='javascript:abc()'><img src='/Images/pen.png'/></td></tr>");
                BindDataChild(node, item.ID, "");
                str.AppendLine(node.ToString());
            }
            str.AppendLine("</table");
            ltr.Text = str.ToString();
            
        }
        protected void BindDataChild(StringBuilder parent, int ParentID, string indent)
        {
            var list = getChildList(ParentID);
            indent += "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
            foreach (var item in list)
            {
                StringBuilder node = new StringBuilder("<tr><td>" + indent + img(item.ID) + item.Name + "</td><td>" + item.ID + "</td><td><a href='javascript:abc()'><img src='/Images/pen.png'/></td></tr>");
                BindDataChild(node, item.ID, indent);
                parent.AppendLine(node.ToString());
            }
        }        
        
        public void Expand(int ID)
        {
            var dataExpand = getChild(ID);
            foreach (var item in dataExpand)
            {
                list.Add(item);
            }
        }

        public void Collapse(int ID)
        {
            var dataCollapse = getChild(ID);
            list.RemoveAll(p=>p.ParentID == ID);
        } 

        protected void btnExpand_Click(object sender, EventArgs e)
        {
            int ID = int.Parse(hdf.Value);
            Expand(ID);
            BindData();
        }

        protected void btnCollapse_Click(object sender, EventArgs e)
        {
            int ID = int.Parse(hdf.Value);
            Collapse(ID);
            BindData();
        }
    }
}